package com.go.where.manager.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.go.where.manager.common.Constants;
import com.go.where.manager.dao.DonateRetrospectDao;
import com.go.where.manager.page.Pagination;
import com.go.where.manager.page.BaseQuery.OrderType;
import com.go.where.manager.pojo.DonateRetrospectInfo;
import com.go.where.manager.pojo.user.UserInfoVO;
import com.go.where.manager.service.DonateRetrospectService;

@Service("donateRetrospectService")
public class DonateRetrospectServiceImpl implements DonateRetrospectService {

	@Resource(name = "donateRetrospectDao")
	private DonateRetrospectDao donateRetrospectDao;

	@Override
	public DonateRetrospectInfo queryByPrimartKey(String cid) {
		return donateRetrospectDao.getByKey(cid);
	}

	@Override
	public Pagination<DonateRetrospectInfo> queryPage(
			DonateRetrospectInfo retorspectInfo) {
		if (StringUtils.isNotEmpty(retorspectInfo.getOrderBy())
				&& retorspectInfo.getOrderType() != null) {
			if (retorspectInfo.getOrderType() == OrderType.asc) {
				retorspectInfo.setPage_orderByAsc(retorspectInfo.getOrderBy());

			} else {
				retorspectInfo.setPage_orderByDesc(retorspectInfo.getOrderBy());
			}
		}
		Integer count = donateRetrospectDao.totalCount(retorspectInfo);
		retorspectInfo.calculate();
		List<DonateRetrospectInfo> list = donateRetrospectDao
				.queryList(retorspectInfo);
		Pagination<DonateRetrospectInfo> page = donateRetrospectDao
				.queryPagination(count, list, retorspectInfo);
		return page;
	}

	@Override
	public int save(DonateRetrospectInfo retorspectInfo, UserInfoVO currentUser) {
		retorspectInfo.setPublishTime(new Date());
		retorspectInfo.setUserId(currentUser.getUserId());
		retorspectInfo.setIsDelete(Constants.NOT_DELETED);
		return donateRetrospectDao.insert(retorspectInfo);
	}

	@Override
	public int update(DonateRetrospectInfo retorspectInfo,
			UserInfoVO currentUser) {
		retorspectInfo.setUpdateTime(new Date());
		retorspectInfo.setUserId(currentUser.getUserId());
		return donateRetrospectDao.update(retorspectInfo);
	}

}
